﻿using System.Collections.Generic;
using System.Linq;
using System.Web.Mvc;
using Telerik.Web.Mvc.UI;
using GKFX.Common.Business.BOL.Common;
using Century.Model;
using GKFX.Common.UI.MVC;
using GKFX.Models.ViewModels;
using Century.EnterpriseLibrary.Configuration;
using GKFX.Business.Repository;

namespace Century.Web.MVC.AdminArea
{
    /// <summary>
    /// Site Tree Controller : Left side tree control
    /// </summary>
    public class SiteTreeController : CenturyAdminBaseController
    {

        string icon_regular = "~/Content/Themes/Admin/img/sitetree/server_go.png";
        string icon_site = "~/Content/Themes/Admin/img/sitetree/site_tree.png";
        string icon_page
        {
            get
            {
                return IsScriptManagerMode ? "~/Content/Themes/Admin/img/sitetree/script_code.png"  : "~/Content/Themes/Admin/img/sitetree/page_white_world.png";
            }
        }
        string icon_homepage = "~/Content/Themes/Admin/img/sitetree/page_white_star.png";
        string icon_page_notpublished = "~/Content/Themes/Admin/img/sitetree/page_red.png";

        public bool IsScriptManagerMode {
            get
            {
                return (ControllerContext.ParentActionViewContext != null && ControllerContext.ParentActionViewContext.RouteData.Values["controller"].ToString() == "CountrySiteScript");
            }
        }

        public ActionResult Index()
        {
            SiteTreeViewModel model = new SiteTreeViewModel();
            //model.Token = Token;
            model.NodeItems = new List<TreeViewItemModel>();

            if (Token.SelectedCountryIndex >= 0)
            {
                model.NodeItems.AddRange(GetSitePages(GKFX.Business.Repository.PersistData.CurrentSite.Id));
            }
            
            //// Add Register - Live Application Node
            //model.NodeItems.Add(new TreeViewItemModel
            //        {
            //            Text = "Register Live Accout",
            //            Value = "RegisterLive",
            //            ImageUrl = icon_regular,
            //            NavigateUrl = PersistData.CurrentSite.LiveApplicationUrl
            //        });
            //// Add Register - Live Application Node
            //model.NodeItems.Add(new TreeViewItemModel
            //{
            //    Text = "Register Demo Account",
            //    Value = "RegisterDemo",
            //    ImageUrl = icon_regular,
            //    NavigateUrl = PersistData.CurrentSite.DemoApplicationUrl
            //});

            //// Add SCA Node
            //model.NodeItems.Add(new TreeViewItemModel
            //{
            //    Text = "Secure Client Area",
            //    Value = "SCA",
            //    ImageUrl = icon_regular,
            //    NavigateUrl = PersistData.CurrentSite.LoginUrl
            //});
            return View(model);
        }
        
        protected TreeViewItemModel GenerateTreeViewNodesForPage(SitePage parentPage)
        {
            TreeViewItemModel node = new TreeViewItemModel();
                List<SitePage> pages = PersistData.AvailablePages.Where(s => s.ParentId == parentPage.Id && s.DraftSourcePageId <= 0).OrderBy(s => s.NodeOrder).ToList();
                if (PersistData.AvailablePages.Any(s => s.ParentId == parentPage.Id))
                {
                    node.Items = new List<TreeViewItemModel>();
                    node.HtmlAttributes["id"] = "first-item-image";
                    node.Text = parentPage.Name;
                    node.Value = string.Format("P-{0}", parentPage.Id.ToString());
                    node.ImageUrl = parentPage.IsPublished ? (parentPage.IsHomePage ? icon_homepage : icon_page) : icon_page_notpublished;
                    if (IsScriptManagerMode)
                    {
                        node.NavigateUrl = Url.Action("PageScript", "CountrySiteScript", new { id = parentPage.Id , area="Admin" });
                    }
                    else
                    {
                        node.NavigateUrl = Url.Action("Get", "PageManagement", new { id = parentPage.Id, area = "Admin" });
                    }

                    foreach (SitePage page in pages)
                    {
                        node.Items.Add(GenerateTreeViewNodesForPage(page));
                    }
                }
                else
                {
                    node = new TreeViewItemModel
                    {
                        Text = parentPage.Name,
                        Value = string.Format("P-{0}", parentPage.Id.ToString()),
                        ImageUrl = parentPage.IsPublished ? (parentPage.IsHomePage ? icon_homepage : icon_page) : icon_page_notpublished,
                        NavigateUrl = (IsScriptManagerMode) ? Url.Action("PageScript", "CountrySiteScript", new { id = parentPage.Id, area = "Admin" }) : Url.Action("Get", "PageManagement", new { id = parentPage.Id, area = "Admin" })
                    };
                    node.HtmlAttributes["id"] = "first-item-image";
                }
            return node;
        }

        protected List<TreeViewItemModel> GetSitePages(int countrySiteId)
        {
            Dictionary<string, string> htmlAttr = new Dictionary<string, string>();
            htmlAttr.Add("class", "t-state-selected");

            List<TreeViewItemModel> items = PersistData.AvailableSites.ToList()
                    .Where(p => p.Id == countrySiteId)
                    .Select(c => new TreeViewItemModel
                    {
                        Text = c.Name,
                        Value = string.Format("CS-{0}", c.Id.ToString()),
                        ImageUrl = icon_site,
                        NavigateUrl = (IsScriptManagerMode) ? Url.Action("PageScript", "CountrySiteScript", new { area = "Admin" }) : Url.Action("Index", "SiteManagement", new { area = "Admin" }),
                        Items = (
                                    PersistData.AvailablePages.Where(x => x.CountrySiteId == c.Id && x.ParentId == null && x.DraftSourcePageId <= 0)
                                    .OrderBy(x => x.NodeOrder)
                                    .Select(
                                                x => new TreeViewItemModel
                                                {
                                                    Checked = true,
                                                    Text = x.Name,
                                                    Value = string.Format("P-{0}", x.Id.ToString()),
                                                    ImageUrl = x.IsPublished ? (x.IsHomePage ? icon_homepage : icon_page) : icon_page_notpublished,
                                                    Items = GenerateTreeViewNodesForPage(x).Items,
                                                    NavigateUrl = (IsScriptManagerMode) ? Url.Action("PageScript", "CountrySiteScript", new { id = x.Id, area = "Admin" }) : Url.Action("Get", "PageManagement", new { id = x.Id, area = "Admin" })
                                                }
                                            ).ToList())
                    }).ToList();

            return items;
        }
    }
}
